package com.example.forum.auto.tests;

import com.example.forum.auto.common.BrowserName;
import com.example.forum.auto.common.Utils;
import lombok.SneakyThrows;
import org.openqa.selenium.By;

/**
 * @author hanzishuai
 * Blog :  https://blog.csdn.net/qrwitu142857
 * @date 2025/5/15 17:12
 * @Description
 */
public class LoginPage extends Utils {
    public static final String url = "http://82.157.200.90:58081/sign-in.html";

    public LoginPage(BrowserName browserName) {
        super(url, browserName);
    }

    public LoginPage() {
        super(url);
    }

    @SneakyThrows
    public void checkPageRight() {
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));
    }

    private void login(String username, String password) {
        // 将输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        // 输入账号密码，并点击提交
        if (username != null) {
            driver.findElement(By.cssSelector("#username")).sendKeys(username);
        }
        if (password != null) {
            driver.findElement(By.cssSelector("#password")).sendKeys(password);
        }

        driver.findElement(By.cssSelector("#submit")).click();
    }

    @SneakyThrows
    public void loginFail() {
        // 不输入账号和密码，点击提交
        login(null, null);


        // 输入账号，不输入密码，点击提交
        login("awa", null);


        // 不输入账号，输入密码，点击提交
        login(null, "123");

        screenShot("LoginFail");

        // 输入账号，输入错误的密码，点击提交
        login("awa", "12");

    }

    @SneakyThrows
    public void loginSuccess() {
        login("awa", "123456");

        // 如果登录成功会进入帖子列表页面
        driver.findElement(By.cssSelector("#article_list_board_title"));
    }
}















